package com.wang.luntan.users.head.controller;

import com.wang.luntan.common.pojo.ApiResponse;
import com.wang.luntan.common.pojo.ApiResponseEnum;
import com.wang.luntan.common.util.ConstatFinalUtil;
import com.wang.luntan.common.web.controller.BaseController;
import com.wang.luntan.users.pojo.AtUsers;
import com.wang.luntan.users.service.INoLoginOperService;
import com.wang.luntan.users.service.IUsersDbService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 不需要登陆的Controller
 *
 * @author wangsh
 */
@Controller
@Log4j2
public class NoLoginHeadController extends BaseController {
    @Resource
    private IUsersDbService usersDbService;
    @Resource
    private INoLoginOperService noLoginOperService;

    /**
     * 打开登陆页面
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/login")
    public String login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("--login--");
        AtUsers users = (AtUsers) this.findObjfromSession(request, "users");
        if (users != null) {
            /* 登陆成功 */
            return "redirect:/head/users/main";
        }
        /* 空的用户 */
        users = new AtUsers();
        request.setAttribute("users", users);
        return "/head/login";
    }

    /**
     * 登陆提交操作
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/loginSubmit")
    public String loginSubmit(HttpServletRequest request, HttpSession session, HttpServletResponse response, Model model) throws IOException {
        log.info("--loginSubmit--");
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        /* 接收参数 */
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String code = request.getParameter("code");

        String randSess = (String) session.getAttribute("randSess");
        Integer failedCount = (Integer) session.getAttribute("failedCount");
        if (failedCount == null) {
            failedCount = 0;
        }

        /* 移除验证码,防止重复使用 */
        session.removeAttribute("randSess");

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("email", email);
        paramsMap.put("password", password);
        paramsMap.put("code", code);
        paramsMap.put("randSess", randSess);
        paramsMap.put("failedCount", failedCount);
        /* 调用Service方法 */
        apiResponse = this.noLoginOperService.operLoginSubmitService(paramsMap);
        if (ApiResponseEnum.STATUS_SUCCESS.getStatus() == apiResponse.getCode()) {
            Map<String, Object> dataMapJava = apiResponse.getDataMapJava();
            /* 登陆成功 */
            AtUsers users = (AtUsers) dataMapJava.get("users");
            Date lastLoginTime = (Date) dataMapJava.get("lastLoginTime");

            session.setAttribute("users", users);
            session.setAttribute("lastLoginTime", lastLoginTime);
            /* 登陆成功 */
            return "redirect:/head/users/main";
        }

        Map<String, Object> dataMapJava = apiResponse.getDataMapJava();
        model.addAttribute("users", dataMapJava.get("users"));

        /* 提示信息 */
        model.addAttribute("response", apiResponse.toJSON());
        return this.login(request, response);
    }

    /**
     * 打开注册页面
     *
     * @return
     */
    @RequestMapping("/register")
    public String register() {
        log.info("--register--");
        return "/head/register";
    }

    /**
     * 注册提交操作
     *
     * @return
     */
    @RequestMapping("/registerSubmit")
    public String registerSubmit(HttpServletRequest request, HttpSession session, Model model) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        log.info("--registerSubmit--");

        /* 接收参数 */
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String randSess = (String) session.getAttribute("randSess");

        /* 移除验证码,防止重复使用 */
        session.removeAttribute("randSess");

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("email", email);
        paramsMap.put("password", password);
        paramsMap.put("code", code);
        paramsMap.put("randSess", randSess);
        /* 调用Service方法 */
        apiResponse = this.noLoginOperService.operRegisterSubmitService(paramsMap);
        if (ApiResponseEnum.STATUS_SUCCESS.getStatus() == apiResponse.getCode()) {
            /* 注册成功,发送邮件 */
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_REGISTER_SUCCESS.getStatus() + ""), Collections.EMPTY_MAP);
        }

        /* 提示信息 */
        model.addAttribute("response", apiResponse.toJSON());
        return this.register();
    }
}
